setwd("~/Desktop/Dissertation DTA")
getwd()

#Installing packages 

library(tidyverse)
install.packages("tidyverse")
install.packages("tidyr")
library(tidyverse)
install.packages("tidyverse")
library(tidyverse)
library(readxl)
install.packages("dplyr")
library(dplyr)
install.packages("writexl")
library(writexl)

##Importing data from desktop

library(readxl)
D3_Framing_Exp_ <- read_excel("~/Desktop/Dissertation DTA/D3_Framing Exp. .xlsx")
View(D3_Framing_Exp_)

##Dataset has 951 observations, after deleting those who failed two attention 
##checks and those who failed one but were outside of one SD from the mean 
##duration time. ##SD was calculated after outliers were omitted. 

D3RawData<-D3_Framing_Exp_
View(D3RawData)

##Changing values to numerics

column.names.num <-names(D3RawData)[c(2)] 
## changed the duration into numerics)

D3RawData1<- mutate_at(D3RawData, column.names.num, as.numeric)
summary(D3RawData1$`Duration (in seconds)`)

View(D3RawData1)
##Changing the other necessary columns to numerics

column.names.num<-names(D3RawData1)[c(4:9,13:50, 51:94)]
D3RawData2<-mutate_at(D3RawData1, column.names.num, as.numeric)


View(D3RawData2)

summary(D3RawData2)

D3_Final_Data
D3RawData2 <- D3_Final_Data

##changing columns to numerics


column.names.num <-names(D3RawData2)[c(2)]
D3RawData3<- mutate_at(D3RawData2, column.names.num, as.numeric)
summary(D3RawData3$`Duration (in seconds)`)

##changing the rest of the columns 

column.names.num<-names(D3RawData3)[c(4:10,14:16, 18:50, 51:63, 65:94, 98:104)]
D3RawData4<-mutate_at(D3RawData3, column.names.num, as.numeric)
##making sure all went well

View(D3RawData4)

D3RawData5 <- D3RawData4
setnames(D3RawData5, old = colnames(D3RawData4), new = gsub("_", ".", 
                                                            colnames(D3RawData4)))
D3RawData5

##now to the plots

install.packages("stats")
library(stats)
library(data.table)
library(dplyr)
library(tidyr)

df1_health <- D3RawData5[44:49]
head(df1_health)
summary(df1_health)
str(df1_health)

library(tidyverse)

##selecting the columns

df_groups =select(df1_health, 1:6)
head(df_groups)

#filtering the (one) Row, but first, changing the names. 

colnames(df_groups)[1] <-"White_People"
colnames(df_groups)[2] <- "Black_People"
colnames(df_groups)[3] <- "All_People"
colnames(df_groups)[4] <- "All_Women"
colnames (df_groups)[5] <- "White_Women"
colnames (df_groups)[6] <-"Black_Women"

df_groups

df_groups<- pivot_longer(df_groups, cols = c("White_People", "Black_People", "All_People", "All_Women",
                                                                "White_Women", "Black_Women"), names_to = "Groups", values_to = "Percentage")
##filtering.

##making a dataframe to filter the rows with gender

df_WhiteWomen = filter(df_groups, Groups == "White_Women")
head(df_WhiteWomen)
dim(df_WhiteWomen)

##WhitePeople

df_WhitePeople  = filter(df_groups, Groups == "White_People")
head(df_WhitePeople)
dim(df_WhitePeople)

## BlackWomen
df_BlackWomen  = filter(df_groups, Groups == "Black_Women")
head(df_BlackWomen)
dim(df_BlackWomen)

##BlackPeople
df_BlackPeople  = filter(df_groups, Groups == "Black_People")
head(df_BlackPeople)
dim(df_BlackPeople)

##AllPeople
df_AllPeople  = filter(df_groups, Groups == "All_People")
head(df_AllPeople)
dim(df_AllPeople)

##All Women
df_AllWomen  = filter(df_groups, Groups == "All_Women")
head(df_AllWomen)
dim(df_AllWomen)

##grouping to summarize the data

df_summzd = group_by(df_groups, Groups) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(df_summzd)

##now plotting

Plot <- ggplot(df_summzd, aes(x = Groups, y = mean, fill = Groups)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd, width = .20))

Plot

#### OK this plot works, but i'd really want to include gender as a categorical variable. 

D3RawData5$Dem.gender[D3RawData5$Dem.gender == 1] <- "Black Men" 
D3RawData5$Dem.gender[D3RawData5$Dem.gender == 2] <- "Black Women"


df_health2 <- data.frame(x = D3RawData5[44:49],
                         y = D3RawData5[4])

view(df_health2)

is.factor(df_health2$Dem.gender)

df_health2$Dem.gender[is.factor(df_health2$Dem.gender)]
summary(df_health2$Dem.gender == "Male")


colnames(df_health2)[1] <-"White People"
colnames(df_health2)[2] <- "Black People"
colnames(df_health2)[3] <- "All People"
colnames(df_health2)[4] <- "All Women"
colnames (df_health2)[5] <- "White Women"
colnames (df_health2)[6] <-"Black Women"
colnames (df_health2)[7] <- "Respondent"

### THIS IS CORRECT FRAME TO USE. Will try with new code.

##filtering rows for all the groups like last time: (INCL Gender) but first going to combine the row into one 
##this way-- if this doesnt work, then I will try the pivot longer function. 

df2_groups<- pivot_longer(df_health2, cols = c("White People", "Black People", "All People", "All Women",
                                             "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

df2_WhitePeople  = filter(df2_groups, Groups == "White People")
head(df_WhitePeople)
dim(df_WhitePeople)

## BlackWomen
df2_BlackWomen  = filter(df2_groups, Groups == "Black Women")
head(df_BlackWomen)
dim(df_BlackWomen)

##BlackPeople
df2_BlackPeople  = filter(df2_groups, Groups == "Black People")
head(df_BlackPeople)
dim(df_BlackPeople)

##AllPeople
df2_AllPeople  = filter(df2_groups, Groups == "All People")
head(df_AllPeople)
dim(df_AllPeople)

##All Women
df2_AllWomen  = filter(df2_groups, Groups == "All Women")
head(df_AllWomen)
dim(df_AllWomen)

##so far looking OK! keep using df2_groups

df2_summzd = group_by(df2_groups, Groups)
head(df2_summzd)

df2_summzd = group_by(df2_groups, Groups, Respondent) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(df2_summzd)

##averages weighted 
df12_summzd = group_by(df2_groups, Groups) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(df12_summzd)


##YES IT LOOKS GREAT. use ##df2_summzd for the chart!!

PlotGender= ggplot(df2_summzd, aes(x = Groups, y = mean, fill = Respondent)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
                labs(title = "Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
                     x = "Group", caption = "sliding scale values from 0 to 100") + 
                  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))


PlotGender 

view(df2_summzd)

## Wonderful. let me make the same graph for MATERNAL health care ## 


df_health3 <- data.frame(x = D3RawData5[50:55],
                         y = D3RawData5[83])

df_health3$`With Gynecologist`[df_health3$`With Gynecologist` == 1] <- "Yes"
df_health3$`With Gynecologist`[df_health3$`With Gynecologist` == 0] <- "No"

View(df_health3)

colnames(df_health3)[1] <-"White People"
colnames(df_health3)[2] <- "Black People"
colnames(df_health3)[3] <- "All People"
colnames(df_health3)[4] <- "All Women"
colnames (df_health3)[5] <- "White Women"
colnames (df_health3)[6] <-"Black Women"
colnames (df_health3)[7] <- "With Gynecologist"

view(df_health3)

### THIS IS CORRECT FRAME TO USE. Will try with new code.

##filtering rows for all the groups like last time: (INCL Gender) but first going to combine the row into one 
##this way-- if this doesnt work, then I will try the pivot longer function. 

df3_groups<- pivot_longer(df_health3, cols = c("White People", "Black People", "All People", "All Women",
                                               "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

df3_WhitePeople  = filter(df3_groups, Groups == "White People")
head(df3_WhitePeople)
dim(df3_WhitePeople)

## BlackWomen
df3_BlackWomen  = filter(df3_groups, Groups == "Black Women")
head(df3_BlackWomen)
dim(df3_BlackWomen)

##BlackPeople
df3_BlackPeople  = filter(df3_groups, Groups == "Black People")
head(df3_BlackPeople)
dim(df3_BlackPeople)

##AllPeople
df3_AllPeople  = filter(df3_groups, Groups == "All People")
head(df3_AllPeople)
dim(df3_AllPeople)

##All Women
df3_AllWomen  = filter(df3_groups, Groups == "All Women")
head(df3_AllWomen)
dim(df3_AllWomen)

##so far looking OK! keep using df3_groups

df3_summzd = group_by(df3_groups, Groups)
head(df3_summzd)

df3_summzd = group_by(df3_groups, Groups, `With Gynecologist`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(df3_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!

PlotGenderBWM= ggplot(df3_summzd, aes(x = Groups, y = mean, fill = `With Gynecologist`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "Black Women: Which Group Benefits Most From the Maternal Health Care", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))

PlotGenderBWM

######### Using data drom D. 2 for ww

approvalraw2$Health_eng_3[is.na(approvalraw2$Health_eng_3)] <- 0

approvalraw2$Health_eng_3[approvalraw2$Health_eng_3 == 1] <- "Yes"
approvalraw2$Health_eng_3[approvalraw2$Health_eng_3 == 0] <- "No"

dfwwd_health <- data.frame(x = approvalraw2[35:40],
                           y = approvalraw2[61])

view(dfwwd_health)


colnames(dfwwd_health)[1] <-"White People"
colnames(dfwwd_health)[2] <- "Black People"
colnames(dfwwd_health)[3] <- "All People"
colnames(dfwwd_health)[4] <- "All Women"
colnames (dfwwd_health)[5] <- "White Women"
colnames (dfwwd_health)[6] <-"Black Women"
colnames (dfwwd_health)[7] <- "W.Doula"

view(dfwwd_health)


dfwwd_groups<- pivot_longer(dfwwd_health, cols = c("White People", "Black People", "All People", "All Women",
                                                   "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

dfwwd_WhitePeople  = filter(dfwwd_groups, Groups == "White People")
head(dfwwd_WhitePeople)
dim(dfwwd_WhitePeople)

## BlackWomen
dfwwd_BlackWomen  = filter(dfwwd_groups, Groups == "Black Women")
head(dfwwd_BlackWomen)
dim(dfwwd_BlackWomen)

##BlackPeople
dfwwd_BlackPeople  = filter(dfwwd_groups, Groups == "Black People")
head(dfwwd_BlackPeople)
dim(dfwwd_BlackPeople)

##AllPeople
dfwwd_AllPeople  = filter(dfwwd_groups, Groups == "All People")
head(dfwwd_AllPeople)
dim(dfwwd_AllPeople)

##All Women
dfwwd_AllWomen  = filter(dfwwd_groups, Groups == "All Women")
head(dfwwd_AllWomen)
dim(dfwwd_AllWomen)

dfwwd_summzd = group_by(dfwwd_groups, Groups)
head(dfwwd_summzd)

dfwwd_summzd = group_by(dfwwd_groups, Groups, `W.Doula`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfwwd_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotHealthExpWWWD= ggplot(dfwwd_summzd, aes(x = Groups, y = mean, fill = `W.Doula`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "White Women: Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))

##They look exactly the same. will show it to ray and ask him if it's worth adding it to our paper or not.

####let me make a visual with those who have a steady experience with health care


view(D3RawData5)

summary(D3RawData5$WHealth.eng.1 == 1)
## there are 456 out of 495 black women that have a gyneologist 

summary(D3RawData5$MHealth.eng.4 == 1)
##theres only 50 men who have a urologist. lets check a primary care physician

summary(D3RawData5$MHealth.eng.1 == 1)
##there are 307 men who have a primary care physician. much better to measure with.but let
##me check for one more.

summary(D3RawData5$MHealth.eng.2 == 1)
##therapist is second most with only 82. so will have to use primary care phys. 
## going to replace all the NA data with 0 in the dataset 

D3RawData5$WHealth.eng.1[is.na(D3RawData5$WHealth.eng.1)] <- 0
D3RawData5$MHealth.eng.1[is.na(D3RawData5$MHealth.eng.1)] <- 0

##ok so all are with 0's now. because it was a conditional question, dont need
##to include gender in the data frame b/c all men and women were given their respective
##questions.so will make a dataframe with these rows and the predis questions... 

##use "fill" call with "WHEALTH and MHEALTH"


view(D3RawData5)

df_health4 <- data.frame(x = D3RawData5[44:49],
                          y = D3RawData5[83])
                         

colnames(df_health4)[1] <-"White People"
colnames(df_health4)[2] <- "Black People"
colnames(df_health4)[3] <- "All People"
colnames(df_health4)[4] <- "All Women"
colnames (df_health4)[5] <- "White Women"
colnames (df_health4)[6] <-"Black Women"
colnames (df_health4)[7] <- "W.Gynecologist"

df_health4

df_health4$W.Gynecologist[df_health4$W.Gynecologist == 1] <- "Yes"
df_health4$W.Gynecologist[df_health4$W.Gynecologist == 0] <- "No"

df4_groups<- pivot_longer(df_health4, cols = c("White People", "Black People", "All People", "All Women",
                                               "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

df4_WhitePeople  = filter(df4_groups, Groups == "White People")
head(df4_WhitePeople)
dim(df4_WhitePeople)

## BlackWomen
df4_BlackWomen  = filter(df4_groups, Groups == "Black Women")
head(df4_BlackWomen)
dim(df4_BlackWomen)

##BlackPeople
df4_BlackPeople  = filter(df4_groups, Groups == "Black People")
head(df4_BlackPeople)
dim(df4_BlackPeople)

##AllPeople
df4_AllPeople  = filter(df4_groups, Groups == "All People")
head(df4_AllPeople)
dim(df4_AllPeople)

##All Women
df4_AllWomen  = filter(df4_groups, Groups == "All Women")
head(df4_AllWomen)
dim(df4_AllWomen)

df4_summzd = group_by(df4_groups, Groups)
head(df4_summzd)

df4_summzd = group_by(df4_groups, Groups, W.Gynecologist) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(df4_summzd)



PlotHealthExpW= ggplot(df4_summzd, aes(x = Groups, y = mean, fill = W.Gynecologist)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "Black Women: Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))

PlotHealthExpW


summary(df4_groups$W.Gynecologist=="Yes")
##92 % of Black women had a gynecologist 


## great now for Black Men.

df_health5 <- data.frame(x = D3RawData5[44:49],
                         y = D3RawData5[89])


colnames(df_health5)[1] <-"White People"
colnames(df_health5)[2] <- "Black People"
colnames(df_health5)[3] <- "All People"
colnames(df_health5)[4] <- "All Women"
colnames (df_health5)[5] <- "White Women"
colnames (df_health5)[6] <-"Black Women"
colnames (df_health5)[7] <- "W.Primary Care Physician"

df_health5

df_health5$`W.Primary Care Physician`[df_health5$`W.Primary Care Physician` == 1] <- "Yes"
df_health5$`W.Primary Care Physician`[df_health5$`W.Primary Care Physician`== 0] <- "No"

df5_groups<- pivot_longer(df_health5, cols = c("White People", "Black People", "All People", "All Women",
                                               "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")


summary(df_health5$`W.Primary Care Physician` == "Yes")
##47% of Black men had a primary care physician



##WhitePeople

df5_WhitePeople  = filter(df5_groups, Groups == "White People")
head(df5_WhitePeople)
dim(df5_WhitePeople)

## BlackWomen
df5_BlackWomen  = filter(df5_groups, Groups == "Black Women")
head(df5_BlackWomen)
dim(df5_BlackWomen)

##BlackPeople
df5_BlackPeople  = filter(df5_groups, Groups == "Black People")
head(df5_BlackPeople)
dim(df5_BlackPeople)

##AllPeople
df5_AllPeople  = filter(df5_groups, Groups == "All People")
head(df5_AllPeople)
dim(df5_AllPeople)

##All Women
df5_AllWomen  = filter(df5_groups, Groups == "All Women")
head(df5_AllWomen)
dim(df5_AllWomen)

df5_summzd = group_by(df5_groups, Groups)
head(df5_summzd)

df5_summzd = group_by(df5_groups, Groups, `W.Primary Care Physician`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(df5_summzd)

view(df5_summzd)




PlotHealthExpM= ggplot(df5_summzd, aes(x = Groups, y = mean, fill = `W.Primary Care Physician`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "Black Men: Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))

PlotHealthExpM
PlotHealthExpW

## going to add both plots in one.

install.packages("patchwork")
library("patchwork")

PlotHealthExpM / PlotHealthExpW

### Sigh, let me do graphs of white women now.

library(readxl)
D2RawData <- read_excel("~/Desktop/Dissertation DTA/D2 full raw data (1).xlsx")
View(D2RawData)

responses <-D2RawData
responses <- as_tibble(responses)
str(responses)

#uploading the CLoudResearch status of observations (rejections/approvals)

D2FullStatusList <- read_excel("~/Desktop/Dissertation DTA/D2 Full status list.xlsx")
View(D2FullStatusList)

##naming as status then as rejects with tibble because it contains the list to reject 

status<- D2FullStatusList
rejects <-as_tibble(status)

status <- status[,-7]
rejects <-as_tibble(status)

##renaming assignment ID from raw dataset

responses <- responses %>% rename(AssignmentId = aid)

view(status)

##filtering out rejected observations from the status dataset 
unique(status$ApprovalStatus)

rejects <- status %>% filter(ApprovalStatus == "Rejected")

##making separate vector with all the IDs of those rejected 

rejectID <- rejects$AssignmentId

#making sure 
rejectID

#removing the rejected ID from the full dataset 
appresponses <- responses %>% filter(! AssignmentId %in% rejectID)

#checking to make sure theres no overlap

inner_join(appresponses, rejects, by = "AssignmentId")
colnames(appresponses)
##checked the column names-- need to change predishealth coding at a later time

##renaming newdataset with rejected ID's 

approvalraw <- appresponses

##double checking 

View(approvalraw)


##more cleaning, deleting unnecessary rows

approvalraw <- approvalraw[, -1]
approvalraw <- approvalraw[, -2]
approvalraw <- approvalraw[, -2]
approvalraw <- approvalraw[, -2]
approvalraw <- approvalraw[, -3]
approvalraw <- approvalraw[, -3]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]
approvalraw <- approvalraw[, -4]

View(approvalraw)

summary(approvalraw)

##currently in character, have to change to numerics, making sure one code
##(duration) works! 

column.names.num <-names(approvalraw)[c(2)]
approvalraw1 <- mutate_at(approvalraw, column.names.num, as.numeric)
summary(approvalraw1$`Duration (in seconds)`)

#changing the rest of the columns to numerics 

column.names.num <- names(approvalraw)[c(4:8, 12:50, 51:64, 67:70)]
approvalraw2 <-mutate_at(approvalraw1, column.names.num, as.numeric)

##example summaries and charts 
summary (approvalraw2)

View(approvalraw2)

##mean age is 44.01, median is 41.00
##mean education is 4.34 (some college but no degree) median is 4 
##mean marital status is 2.34 (married) median is 2 married 
##mean income is 5.23 (between 40k - 49k) median is 5
##PID - mean is 2.18 (Republican)

##NEXT,-- making the graphs with this cleaned data

view(approvalraw2)

summary(approvalraw2$Health_eng_1 == 1)
##861 women have a gynecologist, out of 1047 == 82% of white women have a gynecologist

summary(approvalraw2$Health_eng_3 == 1)
## only 27 have a doula

summary(approvalraw2$Health_eng_2 == 1)
##429 have an obstetrician, which means they have kids. COL 60

approvalraw2$Health_eng_1[is.na(approvalraw2$Health_eng_1)] <- 0
approvalraw2$Health_eng_1[approvalraw2$Health_eng_1 == 1] <- "Yes"
approvalraw2$Health_eng_1[approvalraw2$Health_eng_1 == 0] <- "No"

dfw_health <- data.frame(x = approvalraw2[35:40],
                         y = approvalraw2[59])

view(dfw_health)


colnames(dfw_health)[1] <-"White People"
colnames(dfw_health)[2] <- "Black People"
colnames(dfw_health)[3] <- "All People"
colnames(dfw_health)[4] <- "All Women"
colnames (dfw_health)[5] <- "White Women"
colnames (dfw_health)[6] <-"Black Women"
colnames (dfw_health)[7] <- "W.Gynecologist"

view(dfw_health)


dfwh_groups<- pivot_longer(dfw_health, cols = c("White People", "Black People", "All People", "All Women",
                                               "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

dfw_WhitePeople  = filter(dfwh_groups, Groups == "White People")
head(dfw_WhitePeople)
dim(dfw_WhitePeople)

## BlackWomen
dfw_BlackWomen  = filter(dfwh_groups, Groups == "Black Women")
head(dfw_BlackWomen)
dim(dfw_BlackWomen)

##BlackPeople
dfw_BlackPeople  = filter(dfwh_groups, Groups == "Black People")
head(dfw_BlackPeople)
dim(dfw_BlackPeople)

##AllPeople
dfw_AllPeople  = filter(dfwh_groups, Groups == "All People")
head(dfw_AllPeople)
dim(dfw_AllPeople)

##All Women
dfw_AllWomen  = filter(dfwh_groups, Groups == "All Women")
head(dfw_AllWomen)
dim(dfw_AllWomen)

dfw_summzd = group_by(dfwh_groups, Groups)
head(dfw_summzd)

dfw_summzd = group_by(dfwh_groups, Groups, `W.Gynecologist`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfw_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotHealthExpWW= ggplot(dfw_summzd, aes(x = Groups, y = mean, fill = `W.Gynecologist`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "White Women: Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))

PlotHealthExpWW
PlotHealthExpW

summary(dfw_health$`All People`)
summary(dfw_AllPeople)
summary(df4_AllPeople)
summary(dfw_AllPeople$W.Gynecologist == 1)

## Will try with those who have an obstetrician

approvalraw2$Health_eng_2[is.na(approvalraw2$Health_eng_2)] <- 0

approvalraw2$Health_eng_2[approvalraw2$Health_eng_2 == 1] <- "Yes"
approvalraw2$Health_eng_2[approvalraw2$Health_eng_2 == 0] <- "No"

dfwo_health <- data.frame(x = approvalraw2[35:40],
                         y = approvalraw2[60])

view(dfwo_health)


colnames(dfwo_health)[1] <-"White People"
colnames(dfwo_health)[2] <- "Black People"
colnames(dfwo_health)[3] <- "All People"
colnames(dfwo_health)[4] <- "All Women"
colnames (dfwo_health)[5] <- "White Women"
colnames (dfwo_health)[6] <-"Black Women"
colnames (dfwo_health)[7] <- "W.Obstetrician"

view(dfwo_health)


dfwo_groups<- pivot_longer(dfwo_health, cols = c("White People", "Black People", "All People", "All Women",
                                                "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

dfwo_WhitePeople  = filter(dfwo_groups, Groups == "White People")
head(dfwo_WhitePeople)
dim(dfwo_WhitePeople)

## BlackWomen
dfwo_BlackWomen  = filter(dfwo_groups, Groups == "Black Women")
head(dfwo_BlackWomen)
dim(dfwo_BlackWomen)

##BlackPeople
dfwo_BlackPeople  = filter(dfwo_groups, Groups == "Black People")
head(dfwo_BlackPeople)
dim(dfwo_BlackPeople)

##AllPeople
dfwo_AllPeople  = filter(dfwo_groups, Groups == "All People")
head(dfwo_AllPeople)
dim(dfwo_AllPeople)

##All Women
dfwo_AllWomen  = filter(dfwo_groups, Groups == "All Women")
head(dfwo_AllWomen)
dim(dfwo_AllWomen)

dfwo_summzd = group_by(dfwo_groups, Groups)
head(dfwo_summzd)

dfwo_summzd = group_by(dfwo_groups, Groups, `W.Obstetrician`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfwo_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotHealthExpWWO= ggplot(dfwo_summzd, aes(x = Groups, y = mean, fill = `W.Obstetrician`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "White Women: Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))


PlotHealthExpWWO

## Sill no significant difference. OK here's my last hail mary: those with a doula




approvalraw2$Health_eng_3[approvalraw2$Health_eng_3 == 1] <- "Yes"
approvalraw2$Health_eng_3[approvalraw2$Health_eng_3 == 0] <- "No"

dfwwg_mhealth <- data.frame(x = approvalraw2[41:46],
                          y = approvalraw2[59])

view(dfwwg_mhealth)


colnames(dfwwg_mhealth)[1] <-"White People"
colnames(dfwwg_mhealth)[2] <- "Black People"
colnames(dfwwg_mhealth)[3] <- "All People"
colnames(dfwwg_mhealth)[4] <- "All Women"
colnames (dfwwg_mhealth)[5] <- "White Women"
colnames (dfwwg_mhealth)[6] <-"Black Women"
colnames (dfwwg_mhealth)[7] <- "With Gynecologist"

view(dfwwg_mhealth)


dfwwg_groups<- pivot_longer(dfwwg_mhealth, cols = c("White People", "Black People", "All People", "All Women",
                                                 "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

dfwwg_WhitePeople  = filter(dfwwg_groups, Groups == "White People")
head(dfwwg_WhitePeople)
dim(dfwwg_WhitePeople)

## BlackWomen
dfwwg_BlackWomen  = filter(dfwwg_groups, Groups == "Black Women")
head(dfwwg_BlackWomen)
dim(dfwwg_BlackWomen)

##BlackPeople
dfwwg_BlackPeople  = filter(dfwwg_groups, Groups == "Black People")
head(dfwwg_BlackPeople)
dim(dfwwg_BlackPeople)

##AllPeople
dfwwg_AllPeople  = filter(dfwwg_groups, Groups == "All People")
head(dfwwg_AllPeople)
dim(dfwwg_AllPeople)

##All Women
dfwwg_AllWomen  = filter(dfwwg_groups, Groups == "All Women")
head(dfwwg_AllWomen)
dim(dfwwg_AllWomen)

dfwwg_summzd = group_by(dfwwg_groups, Groups)
head(dfwwg_summzd)

dfwwg_summzd = group_by(dfwwg_groups, Groups, `With Gynecologist`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfwwg_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotMHealthExpWWWG= ggplot(dfwwg_summzd, aes(x = Groups, y = mean, fill = `With Gynecologist`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "White Women: Which Group Benefits Most From Maternal Health Care", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))


PlotHealthExpWWO
PlotHealthExpWWWD
PlotMHealthExpWWWG

 ## wow, still no difference. I can try two more things-- subset the data for 
## white women with children. and check for changes if looking at maternal health.
## will check maternal health first.


approvalraw2$Health_eng_3[is.na(approvalraw2$Health_eng_3)] <- 0

approvalraw2$Health_eng_3[approvalraw2$Health_eng_3 == 1] <- "Yes"
approvalraw2$Health_eng_3[approvalraw2$Health_eng_3 == 0] <- "No"

dfwwd_health <- data.frame(x = approvalraw2[35:40],
                           y = approvalraw2[61])

view(dfwwd_health)


colnames(dfwwd_health)[1] <-"White People"
colnames(dfwwd_health)[2] <- "Black People"
colnames(dfwwd_health)[3] <- "All People"
colnames(dfwwd_health)[4] <- "All Women"
colnames (dfwwd_health)[5] <- "White Women"
colnames (dfwwd_health)[6] <-"Black Women"
colnames (dfwwd_health)[7] <- "W.Doula"

view(dfwwd_health)


dfwwd_groups<- pivot_longer(dfwwd_health, cols = c("White People", "Black People", "All People", "All Women",
                                                   "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

dfwwd_WhitePeople  = filter(dfwwd_groups, Groups == "White People")
head(dfwwd_WhitePeople)
dim(dfwwd_WhitePeople)

## BlackWomen
dfwwd_BlackWomen  = filter(dfwwd_groups, Groups == "Black Women")
head(dfwwd_BlackWomen)
dim(dfwwd_BlackWomen)

##BlackPeople
dfwwd_BlackPeople  = filter(dfwwd_groups, Groups == "Black People")
head(dfwwd_BlackPeople)
dim(dfwwd_BlackPeople)

##AllPeople
dfwwd_AllPeople  = filter(dfwwd_groups, Groups == "All People")
head(dfwwd_AllPeople)
dim(dfwwd_AllPeople)

##All Women
dfwwd_AllWomen  = filter(dfwwd_groups, Groups == "All Women")
head(dfwwd_AllWomen)
dim(dfwwd_AllWomen)

dfwwd_summzd = group_by(dfwwd_groups, Groups)
head(dfwwd_summzd)

dfwwd_summzd = group_by(dfwwd_groups, Groups, `W.Doula`) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfwwd_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotHealthExpWWWD= ggplot(dfwwd_summzd, aes(x = Groups, y = mean, fill = `W.Doula`)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() +
  labs(title = "White Women: Which Group Benefits Most From the Health Care System", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))

## ok for safe keeping, ill do the black women for maternal health with gynecology


dfbwm_health <- data.frame(x = D3RawData5[50:55],
                           y = D3RawData5[83])


D3RawData5$MHealth.eng.1[is.na(D3RawData5$MHealth.eng.1)] <- 0
D3RawData5$MHealth.eng.1[D3RawData5$MHealth.eng.1 == 1] <- "Yes"
D3RawData5$MHealth.eng.1[D3RawData5$MHealth.eng.1 == 0] <- "No"

is.factor(D3RawData5$MHealth.eng.1)
is.numeric(D3RawData5$MHealth.eng.1)


view(dfbwm_health)


colnames(dfbwm_health)[1] <-"White People"
colnames(dfbwm_health)[2] <- "Black People"
colnames(dfbwm_health)[3] <- "All People"
colnames(dfbwm_health)[4] <- "All Women"
colnames (dfbwm_health)[5] <- "White Women"
colnames (dfbwm_health)[6] <-"Black Women"
colnames (dfbwm_health)[7] <- "W.gynecologist"

dfbwm_groups<- pivot_longer(dfbwm_health, cols = c("White People", "Black People", "All People", "All Women",
                                                    "White Women", "Black Women"), names_to = "Groups", values_to = "Percentage")

##WhitePeople

dfbwm_WhitePeople  = filter(dfbwm_groups, Groups == "White People")
head(dfbwm_WhitePeople)
dim(dfbwm_WhitePeople)

## BlackWomen
dfbwm_BlackWomen  = filter(dfbwm_groups, Groups == "Black Women")
head(dfbwm_BlackWomen)
dim(dfbwm_BlackWomen)

##BlackPeople
dfbwm_BlackPeople  = filter(dfbwm_groups, Groups == "Black People")
head(dfbwm_BlackPeople)
dim(dfbwm_BlackPeople)

##AllPeople
dfbwm_AllPeople  = filter(dfbwm_groups, Groups == "All People")
head(dfbwm_AllPeople)
dim(dfbwm_AllPeople)

##All Women
dfbwm_AllWomen  = filter(dfbwm_groups, Groups == "All Women")
head(dfbwm_AllWomen)
dim(dfbwm_AllWomen)

dfbwm_summzd = group_by(dfbwm_groups, Groups)
head(dfbwm_summzd)

dfbwm_summzd = group_by(dfbwm_groups, Groups, W.gynecologist) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfbwm_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotMHealthExpBWWG= ggplot(dfbwm_summzd, aes(x = Groups, y = mean, fill = W.gynecologist)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = mean-sd, ymax=mean+sd), width= 0.20,
                size = .5, position=position_dodge(1)) + theme_bw() + 
  labs(title = "Black Women: Which Group Benefits Most From Maternal Health Care", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic"))


PlotGenderBWM
PlotMHealthExpWWWG

##for maternal health.Let me try to get both plots on the same axis to compare a little more accurately
## Code for Black Women Maternal Health Gynecology

PlotGenderBWM= ggplot(df3_summzd, aes(x = Groups, y = mean, fill = `With Gynecologist`, label= round(mean))) + 
  geom_bar(stat = "identity", position = "dodge") + ylim (0,90) + theme_bw() +
  labs(title = "Black Women's Eval. of Maternal Health Care", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic")) +
  geom_text(position = position_dodge(0.9), vjust=-1)



PlotGenderBWM


##Code for White Women Maternal Health Gynecology

PlotMHealthExpWWWG= ggplot(dfwwg_summzd, aes(x = Groups, y = mean, fill = `With Gynecologist`, label=round(mean))) + 
  geom_bar(stat = "identity", position = "dodge") + ylim(0,90) + theme_bw() +
  labs(title = "White Women's Eval. of Maternal Health Care", y = "Mean 'Percentage' Count",
       x = "Group", caption = "sliding scale values from 0 to 100") + 
  theme(plot.caption = element_text(hjust = 0.5, face = "italic")) +
  geom_text(position = position_dodge(0.9), vjust=-1)

PlotMHealthExpWWWG

##Let me graph it together.

library("patchwork")


PlotMHealthExpWWWG + PlotGenderBWM
  
##There is no difference in relationship with the health care system based on 
## experience for neither Black women or White Women. The evaluations of 
##maternal health as well as health care are also almost identical. However,
##there is a slightly noticeable trend between 

##for White women: largest gap is between white women who do not have a gynecologist
## who believe that Black women are the least likely to benefit from maternal health and
##white women who do have a gynecologist believing that white women 

##DO WE AGREE WITH THE POLICY

##OK so quickly, let me just get the answers to the "health care for all" question.
##Can I combine "healthcare for all" and "Maternal Health" for white women.

##then do another graph with "Health care for all" and "maternal health" Black people.
##Yes I think so. ##going to try it with Black people first.

D3RawData5$treat[D3RawData5$`FL.19.DO.ControlNegative:HealthCareall(NoFraming) `==1] <- 1 
D3RawData5$treat[D3RawData5$`FL.19.DO.ControlPositive:HealthCare(nonrace-gendered)`==1] <-2
D3RawData5$treat[D3RawData5$`FL.19.DO.Treatment:BlackFamilies(racial)  `==1] <- 3
D3RawData5$treat[D3RawData5$`FL.19.DO.Treatment:BlackMaternalHealth(raceandgendered)`==1] <-4 

View(D3RawData5)

D3RawData5$treat[D3RawData5$treat == 1] <- "1.Health Care"
D3RawData5$treat[D3RawData5$treat == 2] <- "Health Care for All"
D3RawData5$treat[D3RawData5$treat == 3] <- "Black Health Care"
D3RawData5$treat[D3RawData5$treat == 4] <-  "Maternal Health Care"

D3RawData5$treat


df_policybp <- data.frame(x = D3RawData5[105],
                           y = D3RawData5[4:22])


df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]
df_policybp <- df_policybp[, -3]

view(df_policybp)

##great, hate that I had to do it this way but alas.


colnames(df_policybp)[1] <-"Policy"
colnames(df_policybp)[2] <- "Gender"
colnames(df_policybp)[3] <- "Evaluation"



##healthcare

df_healthcare  = filter(df_policybp, Policy == "Health Care")
head(df_healthcare)
dim(df_healthcare)

## Negative Health care
df_healthcare  = filter(df_policybp, Policy == "Health Care")
head(df_healthcare)
dim(df_healthcare)

##BlackPeople
dfbwm_BlackPeople  = filter(dfbwm_groups, Groups == "Black People")
head(dfbwm_BlackPeople)
dim(dfbwm_BlackPeople)

##AllPeople
dfbwm_AllPeople  = filter(dfbwm_groups, Groups == "All People")
head(dfbwm_AllPeople)
dim(dfbwm_AllPeople)

##All Women
dfbwm_AllWomen  = filter(dfbwm_groups, Groups == "All Women")
head(dfbwm_AllWomen)
dim(dfbwm_AllWomen)

dfbwm_summzd = group_by(dfbwm_groups, Groups)
head(dfbwm_summzd)

dfbwm_summzd = group_by(dfbwm_groups, Groups, W.gynecologist) %>% summarise(mean=mean(Percentage), sd=sd(Percentage))
head(dfbwm_summzd)

##YES IT LOOKS GREAT. use ##df3_summzd for the chart!!


PlotHealthCarePolicy= ggplot(df_policybp, aes(x = df_policybp, y = Policy == "Health Care", fill = Gender)) + 
  geom_bar(stat = "identity", position = "dodge") + theme_bw() + labs(title = "Support for Better Health Care") 

PlotHealthCarePolicy


